<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ page import="com.smarthome.web.property.utils.UserHolder"  %>
<!DOCTYPE html>
<html>
<head>
<title>物业公司管理管理</title>
<jsp:include page="../../../views/layout/inc.jsp"></jsp:include>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/editPage')}"> --%>
	<script type="text/javascript">
		$.canEdit = true;
	</script>
<%-- </c:if> --%>
<%-- <c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/delete')}"> --%>
	<script type="text/javascript">
		$.canDelete = true;
	</script>
<%-- </c:if> --%>
<script type="text/javascript">
	var dataGrid;
	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			url : '/propertyCompanyController/dataGrid',
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 10,
			pageList : [ 10, 20, 30, 40, 50 ],
			sortName : 'id',
			sortOrder : 'asc',
			singleSelect : true,
			checkOnSelect : true,
			selectOnCheck : true,
			nowrap : true,
			frozenColumns : [ [ {
				field : 'id',
				title : '编号',
				hidden :true,
				width : 150,
				checkbox : true
			}, {
				field : 'code',
				title : '公司编码',
				width : 80,
				sortable : true
			} , {
				field : 'name',
				title : '公司名字',
				width : 80,
				sortable : true
			}] ],
			columns : [ [  {
				field : 'address',
				title :'公司地址',
				width : 150,
				sortable : true
			},{
				field : 'contactPerson',
				title :'公司负责人',
				width : 150,
				sortable : false
			},{
				field : 'mobile',
				title :'手机',
				width : 150,
				sortable : false
			},{
				field : 'tel',
				title :'电话',
				width : 150,
				sortable : false
			},{
				field : 'casePerson',
				title :'紧急联系人',
				width : 150,
				sortable : false
			},{
				field : 'casePersonMobile',
				title :'紧急联系电话',
				width : 150,
				sortable : false
			},{
				field : 'gmtCreate',
				title : '创建时间',
				width : 150,
				sortable : true,
				formatter:function(val,rec){ 
					 var unixTimestamp = new Date(val);  
                     return unixTimestamp.format('yyyy-MM-dd hh:mm:ss');  
				}
			}, {
				field : 'gmtModify',
				title : '最后修改时间',
				width : 150,
				sortable : true,
				formatter:function(val,rec){ 
					 var unixTimestamp = new Date(val);  
                    return unixTimestamp.format('yyyy-MM-dd hh:mm:ss');   
				}
			},{
				field : 'action',
				title : '操作',
				width : 100,
				formatter : function(value, row, index) {
					var str = '';
					if ($.canEdit) {
						str += $.formatString('<img onclick="editFun(\'{0}\');" src="{1}" title="修改"/>', row.id, '/images/extjs_icons/pencil.png');
					}
					str += '&nbsp;';
					if ($.canDelete) {
						str += $.formatString('<img onclick="deleteFun(\'{0}\');" src="{1}" title="删除"/>', row.id, '/images/extjs_icons/cancel.png');
					}
					return str;
				}
			} ] ],
			toolbar : '#toolbar',
			onLoadSuccess : function() {
				$('#searchForm table').show();
				parent.$.messager.progress('close');

				$(this).datagrid('tooltip');
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll').datagrid('uncheckAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});
	});


	function deleteFun(id) {
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {//点击操作里面的删除图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要删除当前公司？', function(b) {
			if (b) {
				parent.$.messager.progress({
					title : '提示',
					text : '数据处理中，请稍后....'
				});
				$.post('/propertyCompanyController/delete', {
					id : id
				}, function(result) {
					if (result.success) {
						dataGrid.datagrid('reload');
					}
					parent.$.messager.alert('提示', result.msg, 'info');
					parent.$.messager.progress('close');
				}, 'JSON');
			}
		});
	}

	function editFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '修改用户',
			width : 500,
			height : 300,
			href : '/propertyCompanyController/editPage?id=' + id,
			buttons : [ {
				text : '确认修改',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}

	function addFun() {
		parent.$.modalDialog({
			title : '新增用户',
			width : 500,
			height : 300,
			href : '/propertyCompanyController/addPage',
			buttons : [ {
				text : '确认新增',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为新增成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#form');
					f.submit();
				}
			} ]
		});
	}


	function searchFun() {
		dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
	}
	function cleanFun() {
		$('#searchForm input').val('');
		dataGrid.datagrid('load', {});
	}
</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="region:'north',title:'查询条件',border:false" style="height: 110px; overflow: hidden;">
			<form id="searchForm">
				<table class="table table-hover table-condensed" style="display: none;">
					<tr>
						<th>公司名字</th>
						<td><input name="name" placeholder="公司名字" class="span2" /></td>
					</tr>
					<tr>
						<th>创建时间</th>
						<td><input class="span2" name="startGmtCreate" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />至<input class="span2" name="endGmtCreate" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" /></td>
					</tr>
					<tr>
						<th>最后修改时间</th>
						<td><input class="span2" name="startGmtModify" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />至<input class="span2" name="endGmtModify" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" /></td>
					</tr>
				</table>
			</form>
		</div>
		<div data-options="region:'center',border:false">
			<table id="dataGrid"></table>
		</div>
	</div>
	<div id="toolbar" style="display: none;">
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/addPage')}"> --%>
		<%if(UserHolder.isSuperResidence()) {%>
			<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
			<%} %>
<%-- 		</c:if> --%>
		<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="searchFun();">提交查询</a>
		<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="cleanFun();">重置查询</a>
	</div>

	<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/addPage')}"> --%>
			<%if(UserHolder.isSuperResidence()) {%>
			<div onclick="addFun();" data-options="iconCls:'pencil_add'">增加</div>
			<%} %>
<%-- 		</c:if> --%>
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/delete')}"> --%>
			<div onclick="deleteFun();" data-options="iconCls:'pencil_delete'">删除</div>
<%-- 		</c:if> --%>
<%-- 		<c:if test="${fn:contains(sessionInfo.resourceList, '/propertyCompanyController/editPage')}"> --%>
			<div onclick="editFun();" data-options="iconCls:'pencil'">修改</div>
<%-- 		</c:if> --%>
	</div>
</body>
</html>